<template>
	<div>
		<main-content
			v-if="pageType === 'i'"
			:tableHeight.sync="tableHeight"
			:otherDependenComponents="otherDependenComponents"
		>
			<template slot="query">
				<el-form
					ref="queryForm"
					:inline="true"
					:model="queryData"
					class="flex-warp-container"
				>
					<el-form-item
						:label="`${$t('common.belong_merchant')}:`"
						prop="merchantId"
						class="flex-row-container"
						label-width="77px"
					>
						<merchant-select
							v-model="queryData.merchantId"
						></merchant-select>
					</el-form-item>
					<el-form-item
						label="结算周期:"
						class="flex-row-container"
						label-width="77px"
					>
						<el-date-picker
							v-model="queryTime"
							size="medium"
							:picker-options="pickerShortcut2"
							format="yyyy-MM-dd"
							type="daterange"
							range-separator="-"
							:start-placeholder="$t('common.start_time')"
							:end-placeholder="$t('common.end_time')"
							align="right"
							:clearable="false"
							:default-time="defaultTime"
						></el-date-picker>
					</el-form-item>

					<el-form-item
						:label="`${$t('funds.fund_audit.lock_status')}:`"
						prop="lockStatus"
						class="flex-row-container"
						label-width="77px"
					>
						<el-select
							v-model="queryData.lockStatus"
							style="width: 200px"
							:placeholder="$t('common.select_all')"
							clearable
						>
							<el-option
								v-for="item in lockOrderType"
								:key="item.code"
								:label="item.description"
								:value="item.code"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item
						label="业务模式:"
						class="flex-row-container"
						label-width="77px"
					>
						<el-select
							v-model="queryData.businessModel"
							size="medium"
							:placeholder="$t('common.select_all')"
							style="width: 200px"
							clearable
						>
							<el-option
								v-for="item in businessModelList"
								:key="item.code"
								:label="item.description"
								:value="item.code"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item class="flex-row-container ptop">
						<el-select
							v-model="dateFlag"
							style="width:140px"
							:placeholder="$t('common.please_choose')"
						>
							<el-option label="代理账号" value="0" />
							<el-option label="总代账号" value="1" />
							<el-option label="订单号" value="2" />
						</el-select>
						<el-input
							v-if="dateFlag == '0'"
							v-model.trim="queryData.proxyName"
							clearable
							style="width:180px"
							:placeholder="$t('common.please_enter')"
							v-input="{ name: 'num_alp' }"
						></el-input>
						<el-input
							v-if="dateFlag == '1'"
							v-model.trim="queryData.topProxyName"
							clearable
							style="width:180px"
							:placeholder="$t('common.please_enter')"
							v-input="{ name: 'num_alp' }"
						></el-input>
						<el-input
							v-if="dateFlag == '2'"
							v-model.trim="queryData.orderNo"
							clearable
							style="width:180px"
							:placeholder="$t('common.please_enter')"
							v-input="{ name: 'num_alp' }"
						></el-input>
					</el-form-item>
					<el-form-item
						:label="$t('report.agency_level')"
						class="flex-row-container"
						label-width="77px"
					>
						<el-select
							v-model="queryData.proxyLevel"
							size="medium"
							:placeholder="$t('common.select_all')"
							clearable
							multiple
							collapse-tags
						>
							<el-option
								v-for="item in proxyleveList"
								:key="item.id"
								:label="item.id"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
					<!-- <el-form-item label="审核员:" prop="lockAccount">
						<el-input
							v-model="queryData.lockAccount"
							clearable
							size="medium"
							:maxlength="11"
							style="width: 220px"
							:placeholder="$t('common.please_enter')"
						></el-input>
					</el-form-item> -->
					<el-form-item
						label="实发收益"
						prop="lockAccount"
						class="flex-row-container"
						label-width="77px"
					>
						<el-input
							v-model="queryData.minIncomeAmount"
							size="medium"
							clearable
							style="width: 130px"
							:placeholder="$t('common.minimum')"
							maxLength="15"
							v-input="{ name: 'onlyTwoNum' }"
							name="minIncomeAmount"
							@blur="
								handleAmount(
									queryData,
									1,
									'minIncomeAmount',
									'maxIncomeAmount',
									$t(
										'funds.proxy_member_funds_record.form_search_rebateCheck1'
									)
								)
							"
						></el-input>
						-
						<el-input
							v-model="queryData.maxIncomeAmount"
							size="medium"
							clearable
							style="width: 130px"
							:placeholder="$t('common.maximum')"
							maxLength="15"
							v-input="{ name: 'onlyTwoNum' }"
							name="maxIncomeAmount"
							@blur="
								handleAmount(
									queryData,
									2,
									'minIncomeAmount',
									'maxIncomeAmount',
									$t(
										'funds.proxy_member_funds_record.form_search_rebateCheck2'
									)
								)
							"
						></el-input>
					</el-form-item>
					<!-- <el-form-item
						class="flex-row-container"
						:label="`${$t('funds.proxy_name')}:`"
						label-width="77px"
					>
						<el-input
							v-model="queryData.proxyName"
							:placeholder="$t('common.please_enter')"
							clearable
							style="width: 220px"
							size="medium"
							v-input="{ name: 'num_alp' }"
						></el-input>
					</el-form-item> -->
					<el-form-item style="margin-left: 8px">
						<el-button
							type="primary"
							icon="el-icon-search"
							size="medium"
							@click="search"
						>
							{{ $t('common.search') }}
						</el-button>
						<el-button
							icon="el-icon-refresh-left"
							size="medium"
							@click="reset"
						>
							{{ $t('common.reset') }}
						</el-button>
						<!-- <el-button
							type="success"
							icon="el-icon-sort"
							:disabled="loading"
							size="medium"
							@click="openColSettings"
						>
							{{ $t('common.row_set') }}
						</el-button> -->
					</el-form-item>
				</el-form>
				<!-- <div class="divBox">
					<p class="danger data-refresh">
						数据刷新时间：
						{{ now }}
					</p>
					<el-select
						v-model="activeRebate"
						size="mini"
						:placeholder="$t('common.please_choose')"
						@change="recordTime"
					>
						<el-option
							v-for="item in timeList"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</div> -->
			</template>
			<template slot="content">
				<div
					v-if="
						hasPermission('90570055') || hasPermission('90570056')
					"
				>
					<el-tabs v-model="activeName" @tab-click="switchPage">
						<el-tab-pane
							v-if="hasPermission('90570055')"
							:label="$t('funds.fund_audit.wait_audit_1')"
							name="1"
						></el-tab-pane>
						<el-tab-pane
							v-if="hasPermission('90570056')"
							:label="$t('funds.fund_audit.wait_audit_2')"
							name="2"
						></el-tab-pane>
					</el-tabs>
					<el-table
						v-loading="loading"
						element-loading-spinner="el-icon-loading"
						border
						size="mini"
						class="small-size-table"
						:data="tableData"
						style="width: 100%"
						highlight-current-row
						:header-cell-style="getRowClass"
						:max-height="tableHeight"
					>
						<el-table-column
							align="center"
							:label="$t('funds.fund_audit.lock')"
							min-width="60"
						>
							<template slot-scope="scope">
								<el-checkbox
									v-if="hasPermission('90570057')"
									v-model="scope.row.lockOrder"
									:disabled="!showLock(scope.row)"
									@change="lockOne($event, scope.row.orderNo)"
								></el-checkbox>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="auditStep"
							align="center"
							:label="$t('common.actions')"
							min-width="150"
						>
							<template slot-scope="scope">
								<el-button
									:class="
										scope.row.auditName !== userName
											? 'dis'
											: ''
									"
									type="primary"
									size="medium"
									@click="openDetail(scope.row)"
								>
									{{ $t('funds.fund_audit.audit') }}
								</el-button>
							</template>
						</el-table-column>

						<el-table-column
							prop="orderNo"
							align="center"
							:label="$t('funds.order_no')"
							min-width="280"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy :title="scope.row.orderNo" :copy="copy">
									{{ scope.row.orderNo }}
								</Copy>
							</template>
						</el-table-column>
						<el-table-column
							align="center"
							label="结算周期"
							min-width="200"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{
									getRebateCycle(
										scope.row.startTime,
										scope.row.endTime
									)
								}}
								<!-- {{ scope.row.startTime }} - {{  scope.row.endTime }} -->
							</template>
						</el-table-column>
						<el-table-column
							prop="proxyName"
							align="center"
							:label="$t('funds.proxy_name')"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy :title="scope.row.proxyName" :copy="copy">
									{{ scope.row.proxyName }}
								</Copy>
							</template>
						</el-table-column>
						<el-table-column
							prop="userName"
							align="center"
							label="业务模式"
						>
							<template slot-scope="scope">
								{{
									mappingBusinessModel(
										scope.row.businessModel
									)
								}}
							</template>
						</el-table-column>
						<el-table-column
							prop="proxyLevel"
							align="center"
							:label="
								$t(
									'funds.proxy_member_funds_record.proxy_level_name'
								)
							"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{ scope.row.proxyLevel }}
							</template>
						</el-table-column>
						<el-table-column
							prop="proxyLevelName"
							align="center"
							label="收益比例"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span style="visibility:visible;">
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 0)"
									>
										查看
									</el-link>
								</span>
							</template>
						</el-table-column>

						<el-table-column
							prop="merchantName"
							align="center"
							:label="
								$t(
									'funds.proxy_member_funds_record.parent_proxy_account'
								)
							"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span v-if="scope.row.parentProxyName === '-'">
									{{ scope.row.parentProxyName }}
								</span>
								<Copy
									v-else
									:title="scope.row.parentProxyName"
									:copy="copy"
								>
									{{ scope.row.parentProxyName }}
								</Copy>
							</template>
						</el-table-column>
						<el-table-column
							prop="topProxyName"
							align="center"
							:label="
								$t(
									'funds.proxy_member_funds_record.top_proxy_name'
								)
							"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy
									:title="scope.row.topProxyName"
									:copy="copy"
								>
									{{ scope.row.topProxyName }}
								</Copy>
							</template>
						</el-table-column>

						<!-- <el-table-column
							prop="merchantName"
							align="center"
							:label="$t('common.belong_merchant')"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{ merchantFilter(scope.row.merchantId) }}
							</template>
						</el-table-column> -->

						<el-table-column
							prop="dxServiceFeeRebateAmount"
							align="center"
							label="德州俱乐部服务费分成"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									:style="
										handleNumberColor(
											scope.row.dxServiceFeeRebateAmount
										)
									"
								>
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 1)"
									>
										{{
											handleTotalNumber(
												'USDT',
												scope.row
													.dxServiceFeeRebateAmount
											)
										}}
									</el-link>
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="dxInsurancePartsAmount"
							align="center"
							label="德州保险盈亏分成"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									:style="
										handleNumberColor(
											scope.row.dxInsurancePartsAmount
										)
									"
								>
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 2)"
									>
										{{
											handleTotalNumber(
												'USDT',
												scope.row.dxInsurancePartsAmount
											)
										}}
									</el-link>
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="dxInsurancePartsAmount"
							align="center"
							label="德州增值服务分成"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									:style="
										handleNumberColor(
											scope.row.valueAddedTotalAmount
										)
									"
								>
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 5)"
									>
										{{
											handleTotalNumber(
												'USDT',
												scope.row.valueAddedTotalAmount
											)
										}}
									</el-link>
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="otherVenueRebateAmount"
							align="center"
							label="其他场馆代理返点"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									:style="
										handleNumberColor(
											scope.row.otherVenueRebateAmount
										)
									"
								>
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 3)"
									>
										{{
											handleTotalNumber(
												'USDT',
												scope.row.otherVenueRebateAmount
											)
										}}
									</el-link>
								</p>
							</template>
						</el-table-column>
						<!-- <el-table-column
							prop="otherVenuePartsAmount"
							align="center"
							label="其他场馆盈亏占成"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									v-if="scope.row.billFlag"
									:style="
										handleNumberColor(
											scope.row.otherVenuePartsAmount
										)
									"
								>
									<el-link
										class="lightBlueColor"
										type="primary"
										@click="dialogData(scope.row, 4)"
									>
										{{
											handleTotalNumber(
												'USDT',
												scope.row.otherVenuePartsAmount
											)
										}}
									</el-link>
								</p>
								<p v-else style="color:#CCCCCC;">
									{{
										handleTotalNumber(
											'USDT',
											scope.row.otherVenuePartsAmount
										)
									}}
								</p>
							</template>
						</el-table-column> -->
						<el-table-column
							prop="currentIncomeAmount"
							align="center"
							label="本期收益"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p>
									{{
										handleTotalNumber(
											'USDT',
											scope.row.currentIncomeAmount
										)
									}}
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="adjustIncomeAmount"
							align="center"
							label="收益调整"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p
									:style="
										handleNumberColor(
											scope.row.adjustIncomeAmount
										)
									"
								>
									{{
										handleTotalNumber(
											'USDT',
											scope.row.adjustIncomeAmount
										)
									}}
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="realIncomeAmount"
							align="center"
							label="调整后收益"
							show-overflow-tooltip
							min-width="200"
						>
							<template slot-scope="scope">
								<p>
									{{
										handleTotalNumber(
											'USDT',
											scope.row.realIncomeAmount
										)
									}}
								</p>
							</template>
						</el-table-column>
						<el-table-column
							prop="lockStatus"
							align="center"
							:label="$t('funds.fund_audit.audit_status')"
							min-width="180"
						>
							<template slot-scope="scope">
								<span
									:class="
										Number(scope.row.lockStatus) === 1
											? 'lockingRgba'
											: 'deleteRgba'
									"
								>
									{{
										typeFilter(
											scope.row.lockStatus,
											'auditStatus'
										)
									}}
								</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="lockTime"
							align="center"
							:label="$t('funds.fund_audit.lock_time')"
							min-width="220"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span v-if="!!scope.row.lockTime">
									{{ scope.row.lockTime | getTime }}
								</span>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="auditName"
							align="center"
							:label="$t('funds.fund_audit.lock_account')"
							min-width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{ scope.row.auditName || '-' }}
							</template>
						</el-table-column>
						<el-table-column
							prop="applyTime"
							align="center"
							:label="$t('funds.apply_time')"
							min-width="180"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span v-if="scope.row.applyTime">
									{{ scope.row.applyTime | getTime }}
								</span>
								<span v-else>-</span>
							</template>
						</el-table-column>
					</el-table>
					<el-row v-if="tableData.length">
						<el-col :span="24">
							<div class="grid-content">
								<el-checkbox
									v-if="hasPermission('90570058')"
									v-model="lockedAllStatus"
									:label="$t('common.all')"
									name="type"
									:disabled="lockedAllDisabled"
									@change="lockAll"
								></el-checkbox>
								<el-button
									v-if="hasPermission('90570060')"
									type="primary"
									size="medium"
									style="margin-left: 20px"
									:disabled="lockedArr.length === 0"
									@click="openBatchReviewDialog"
								>
									{{ $t('funds.fund_audit.batcha_audit') }}
								</el-button>
							</div>
						</el-col>
					</el-row>
					<!-- 分页 -->
					<el-pagination
						v-if="
							hasPermission('90570055') ||
								hasPermission('90570056')
						"
						:current-page.sync="pageNum"
						class="pageValue"
						v-show="!!total"
						background
						layout="total, sizes,prev, pager, next, jumper"
						:page-size="pageSize"
						:page-sizes="pageSizes"
						:total="total"
						:pager-count="9"
						@current-change="handleCurrentChange"
						@size-change="handleSizeChange"
					></el-pagination>
				</div>
				<el-empty
					v-else
					:description="$t('funds.jurisdiction_tip')"
				></el-empty>
			</template>
		</main-content>
		<detail
			v-else
			:allow-audit="allowAudit"
			:detail-data="detailData"
			:activeName="activeName"
			@goBack="goBack"
		></detail>
		<el-dialog
			class="batch-review"
			:title="$t('funds.fund_audit.batcha_audit')"
			width="700px"
			:visible.sync="batchReviewDialog.visible"
			@close="onBatchReviewDialogClose"
		>
			<template>
				<div class="review-wrapper">
					<div class="top">
						<el-row>
							<el-col :span="12" class="title border-right">
								{{ $t('funds.fund_audit.audit_number') }}
							</el-col>
							<el-col :span="12" class="title">
								{{ $t('funds.fund_audit.audit_amount') }}
							</el-col>
							<el-col :span="12" class="border-right">
								<el-link
									type="primary"
									@click="batchReviewDialog.pageType = 'd'"
								>
									{{ batchReviewDialog.totalRecords }}
								</el-link>
							</el-col>
							<el-col :span="12" class="amounts-list">
								<div>
									<div
										v-for="item in Object.keys(
											batchReviewDialog.amounts
										)"
										:key="item"
									>
										{{ handleCurrency(item) }}
										{{
											handleNumber(
												item,
												batchReviewDialog.amounts[item]
											)
										}}
									</div>
								</div>
							</el-col>
						</el-row>
					</div>
					<div>
						<div
							v-if="batchReviewDialog.pageType === 'i'"
							class="bottom"
						>
							<el-form
								ref="batchAuditForm"
								:model="batchReviewDialog.formData"
								:rules="rules"
							>
								<div class="remark-wrapper">
									<div class="left">
										<span>
											{{
												$t(
													'funds.fund_audit.audit_remark'
												)
											}}:
										</span>
									</div>
									<div class="right">
										<el-form-item prop="remark">
											<el-input
												v-model.trim="
													batchReviewDialog.formData
														.remark
												"
												type="textarea"
												:rows="6"
												maxlength="500"
												show-word-limit
												v-input="{ name: 'noEmpty' }"
												:placeholder="
													$t(
														'funds.fund_audit.form_remark_placeholder'
													)
												"
												resize="none"
											></el-input>
										</el-form-item>
									</div>
								</div>
							</el-form>
						</div>
						<div v-else>
							<el-table
								v-loading="batchReviewDialog.loading"
								border
								height="311"
								size="mini"
								class="small-size-table"
								:data="lockedArr"
								style="width: 100%"
								:header-cell-style="{ background: '#e6e6e6' }"
							>
								<el-table-column
									align="center"
									type="index"
									:label="$t('common.serial_number')"
								></el-table-column>
								<el-table-column
									align="center"
									:label="$t('funds.proxy_name')"
									prop="proxyName"
								></el-table-column>
								<el-table-column
									align="center"
									label="收益金额"
									prop="realIncomeAmount"
								>
									<template slot-scope="scope">
										{{ handleCurrency(scope.row.currency) }}
										{{
											handleNumber(
												scope.row.currency,
												scope.row.realIncomeAmount
											)
										}}
									</template>
								</el-table-column>
							</el-table>
						</div>
					</div>
				</div>
				<div slot="footer">
					<div
						v-if="batchReviewDialog.pageType === 'i'"
						class="center-wrap"
					>
						<el-button
							:disabled="batchReviewDialog.loading"
							size="small"
							@click="batchReviewDialog.visible = false"
						>
							{{ $t('common.cancel') }}
						</el-button>
						<el-button
							:disabled="batchReviewDialog.loading"
							type="success"
							size="small"
							@click="openSeaMoonDialog(true, 1)"
						>
							<i
								v-show="
									batchReviewDialog.loading &&
										batchReviewDialog.btnType === 1
								"
								class="el-icon-loading"
							></i>
							{{
								activeName === '1'
									? $t('funds.fund_audit.audit_success_1')
									: $t('funds.fund_audit.audit_success_2')
							}}
						</el-button>
						<el-button
							:disabled="batchReviewDialog.loading"
							type="danger"
							size="small"
							@click="openSeaMoonDialog(true, 2)"
						>
							<i
								v-show="
									batchReviewDialog.loading &&
										batchReviewDialog.btnType === 2
								"
								class="el-icon-loading"
							></i>
							{{
								activeName === '1'
									? $t('funds.fund_audit.audit_fail_1')
									: $t('funds.fund_audit.audit_fail_2')
							}}
						</el-button>
					</div>
					<div v-else class="center-wrap">
						<el-button
							size="small"
							type="primary"
							@click="batchReviewDialog.pageType = 'i'"
						>
							{{ $t('common.sure') }}
						</el-button>
					</div>
				</div>
			</template>
		</el-dialog>
		<SeaMoonDialog
			:sea-moon-visible.sync="seaMoon.visible"
			:params="seaMoon.params"
			@success="onSeaMoonValidSucceed"
		></SeaMoonDialog>
		<el-dialog
			:title="$t('common.row_set')"
			center
			:visible.sync="colSettings.visible"
			width="475px"
			class="setting-div"
		>
			<div>
				<el-link type="primary" class="mb15" @click="clickDel">
					复原缺省
				</el-link>
			</div>
			<div
				v-for="(value, name1) in activityListField"
				:key="name1"
				class="setting-div"
			>
				<el-checkbox v-model="dialogColumnData[name1]">
					{{ name1 }}
				</el-checkbox>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="colSettings.visible = false">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button type="primary" @click="handleOrderList">
					{{ $t('common.submit') }}
				</el-button>
			</div>
		</el-dialog>

		<!-- 收益比例弹窗 -->
		<IncomeDetailDialog
			v-if="incomeDetailDialogVisible"
			:visible.sync="incomeDetailDialogVisible"
			@handleVisible="incomeDetailDialogVisible = false"
			:curData="curData"
		/>

		<!-- 德州服务费返点弹窗 -->
		<AuditDetailDialog1
			v-if="auditDetailDialogVisible1"
			:visible.sync="auditDetailDialogVisible1"
			@handleVisible="auditDetailDialogVisible1 = false"
			:curData="curData"
		/>

		<!-- 德州保险分成弹窗 -->
		<AuditDetailDialog2
			v-if="auditDetailDialogVisible2"
			:visible.sync="auditDetailDialogVisible2"
			@handleVisible="auditDetailDialogVisible2 = false"
			:curData="curData"
		/>

		<!-- 其他场馆返点弹窗 -->
		<AuditDetailDialog3
			v-if="auditDetailDialogVisible3"
			:visible.sync="auditDetailDialogVisible3"
			@handleVisible="auditDetailDialogVisible3 = false"
			:curData="curData"
		/>

		<!-- 其他场馆分成弹窗 -->
		<AuditDetailDialog4
			v-if="auditDetailDialogVisible4"
			:visible.sync="auditDetailDialogVisible4"
			@handleVisible="auditDetailDialogVisible4 = false"
			:curData="curData"
		/>

		<!-- 增值分成弹窗 -->
		<AuditDetailDialog5
			v-if="auditDetailDialogVisible5"
			:visible.sync="auditDetailDialogVisible5"
			@handleVisible="auditDetailDialogVisible5 = false"
			:curData="curData"
		/>
	</div>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { getUsername, getUserInfo } from '@/utils/auth'
import SeaMoonDialog from '@/components/SeaMoon'
import { Decimal } from 'decimal.js'
import MainContent from '@/components/MainContent/index.vue'
import IncomeDetailDialog from '@/components/Funds/ProxyIncome/incomeDetailDialog.vue'
import AuditDetailDialog1 from '@/components/Funds/ProxyIncome/auditDetailDialog1.vue'
import AuditDetailDialog2 from '@/components/Funds/ProxyIncome/auditDetailDialog2.vue'
import AuditDetailDialog3 from '@/components/Funds/ProxyIncome/auditDetailDialog3.vue'
import AuditDetailDialog4 from '@/components/Funds/ProxyIncome/auditDetailDialog4.vue'
import AuditDetailDialog5 from '@/components/Funds/ProxyIncome/auditDetailDialog5.vue'
const end = () => {
	return dayjs()
		.add(-1, 'day')
		.endOf('day')
		.valueOf()
}
const start = () => {
	return dayjs()
		.add(-1, 'day')
		.startOf('day')
		.valueOf()
}
const name = 'rebateCheck'
export default {
	name: name,
	components: {
		detail: () => import('./components/detail'),
		SeaMoonDialog,
		MainContent,
		IncomeDetailDialog,
		AuditDetailDialog1,
		AuditDetailDialog2,
		AuditDetailDialog3,
		AuditDetailDialog4,
		AuditDetailDialog5
	},
	filters: {
		getTime(value) {
			return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
		}
	},
	mixins: [list],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: ['reviewBtn', 'reviewTab'],
			queryData: {
				minIncomeAmount: undefined,
				maxIncomeAmount: undefined
			},
			queryTime: [start(), end()],
			allowAudit: false,
			pageType: 'i',
			detailData: {},
			now: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
			curData: {},
			incomeDetailDialogVisible: false,
			auditDetailDialogVisible1: false,
			auditDetailDialogVisible2: false,
			auditDetailDialogVisible3: false,
			auditDetailDialogVisible4: false,
			auditDetailDialogVisible5: false,
			userName: '',
			activeName: '1',
			dateFlag: '0',
			tableData: [],
			proxyleveList: [],
			businessModelList: [
				{
					description: '现金业务',
					code: '1'
				},
				{
					description: '信用业务',
					code: '2'
				}
			],
			lockedAllStatus: false,
			lockedAllDisabled: false,
			lockedArr: [],
			batchReviewDialog: {
				pageType: 'i',
				visible: false,
				btnType: '',
				loading: false,
				totalRebateAmount: 0,
				totalRecords: 0,
				amounts: {},
				formData: {
					remark: undefined,
					ids: []
				}
			},
			showClear1: false,
			showClear2: false,
			rules: {
				remark: [
					{
						required: true,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					},
					{
						min: 2,
						max: 500,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					}
				]
			},
			timeList: [],
			rebateTimer: null,
			activeRebate: this.$t('funds.fund_audit.no_reload'),
			seaMoon: {
				isAudit: false,
				visible: false,
				params: {}
			},
			highTable: 0,
			colSettings: {
				visible: false,
				dialogColumnData: {}
			},
			activityListField: {
				// 锁单: true,
				// 订单号: true,
				// 返点周期: true,
				// 代理账号: true,
				// 所属商户: true,
				// 返点金额: true,
				// 审核状态: true,
				// 锁单时间: true,
				// 审核人: true,
				// 申请时间: true
				[this.$t('funds.fund_audit.lock')]: true,
				[this.$t('funds.order_no')]: true,
				[this.$t('funds.apply_time')]: true,
				[this.$t('funds.fund_audit.rebates_cycle')]: true,
				[this.$t('funds.proxy_name')]: true,
				[this.$t('common.belong_merchant')]: true,
				[this.$t('funds.fund_audit.amount_of_rebates')]: true,
				[this.$t('funds.fund_audit.audit_status')]: true,
				[this.$t('funds.fund_audit.lock_time')]: true,
				[this.$t('funds.fund_audit.lock_account')]: true,
				[this.$t('funds.apply_time')]: true
			}
		}
	},
	computed: {
		lockOrderType() {
			return this.globalDics.lockStatus || []
		},
		orderStatusArr() {
			//  0-待一审 1-一审拒绝 2-待二审 3-二审拒绝 3-二审通过
			return [
				{
					code: '0',
					description: this.$t('funds.fund_audit.wait_audit_1')
				},
				{
					code: '1',
					description: this.$t('funds.fund_audit.audit_fail_1')
				},
				{
					code: '2',
					description: this.$t('funds.fund_audit.wait_audit_2')
				},
				{
					code: '3',
					description: this.$t('funds.fund_audit.audit_fail_2')
				},
				{
					code: '4',
					description: this.$t('funds.fund_audit.pass_the_audit')
				}
			]
		}
	},
	watch: {
		// 'queryData.minIncomeAmount'() {
		// 	if (
		// 		this.queryData.minIncomeAmount > this.queryData.maxIncomeAmount
		// 	) {
		// 		this.queryData.maxIncomeAmount =
		// 			this.queryData.minIncomeAmount + 0.01
		// 	}
		// }
	},
	created() {
		this.initIndexDB('rebateCheck', this.activityListField)
		if (this.hasPermission('90570055') || this.hasPermission('90570056')) {
			this.getKvconfigList()
		}
		if (this.hasPermission('90570055')) {
			this.activeName = '1'
		} else if (this.hasPermission('90570056')) {
			this.activeName = '2'
		} else {
			this.activeName = ''
		}
		this.userName = getUsername()
		this.userInfo = getUserInfo()
		this.getProxyleve()
	},

	activated() {
		if (this.hasPermission('90570055') || this.hasPermission('90570056')) {
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			} else {
				clearInterval(this.rebateTimer)
			}
		}
	},
	deactivated() {
		clearInterval(this.rebateTimer)
	},
	beforeDestroy() {
		clearInterval(this.rebateTimer)
		this.rebateTimer = null
	},
	mounted() {
		// this.calHeight()
		// const _self = this
		// window.onresize = () => {
		// 	_self.calHeight()
		// }
	},
	methods: {
		calHeight() {
			this.$nextTick(() => {
				const rect = this.$refs.container?.getBoundingClientRect()
				if (rect?.height) {
					this.highTable = window.innerHeight - rect.height - 350
				}
			})
		},
		hiddenClearIcon(key) {
			setTimeout(() => {
				this[key] = false
			}, 200)
		},
		clearable(key) {
			this.$delete(this.queryData, key)
		},
		onSeaMoonValidSucceed(data) {
			if (this.seaMoon.isAudit) {
				this.handleBatchReview()
			}
		},
		// isAudit: 是否是审核操作
		// type: 1 通过  2 拒绝
		openSeaMoonDialog(isAudit, type) {
			this.$refs.batchAuditForm.validate((valid) => {
				if (valid) {
					this.batchReviewDialog.btnType = type
					this.handleBatchReview()
				}
			})
		},
		search() {
			this.pageNum = 1
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData(),
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		getProxyleve() {
			this.$api.getProxylevelnameList().then((res) => {
				this.proxyleveList = res.data || []
			})
		},
		getOrderStatusColor(row) {
			if (row.lockStatus === 1) {
				return 'lockingRgba'
			} else if ([0, 2].includes(Number(row.orderStatus))) {
				return 'disableRgba'
			}
		},
		recordTime(val) {
			this.activeRebate = val

			if (Number(val)) {
				clearInterval(this.rebateTimer)
				this.rebateTimer = setInterval(this.loadData, val * 1000)
			} else {
				clearInterval(this.rebateTimer)
			}
		},
		getKvconfigList() {
			this.timeList = []
			let str = ''
			let arr = []
			const params = {
				k: 'funds.audit.pagerefresh.interval.config',
				pageNum: 1,
				pageSize: 10
			}
			this.$api.getkvconfigQueryList(params).then((res) => {
				if (res.code === 200 && res.data) {
					const val = res.data.records[0] || []
					if (val.v) {
						str = val.v
						arr = str.split(',').sort()
						arr.forEach((item, index) => {
							const obj = {}
							if (Number(item) === 0) {
								obj.label = this.$t(
									'funds.fund_audit.no_reload'
								)
								obj.value = this.$t(
									'funds.fund_audit.no_reload'
								)
								this.timeList.push(obj)
								this.activeRebate = this.$t(
									'funds.fund_audit.no_reload'
								)
								this.rebateTimer = null
							} else if (Number(item)) {
								obj.label =
									item + this.$t('funds.fund_audit.second')
								obj.value = item
								this.timeList.push(obj)
							}
							// if (Number(item) && index === 1) {
							// 	this.activeRebate = item
							// 	this.rebateTimer = setInterval(this.loadData, item * 1000)
							// }
						})
					}
				}
			})
		},
		getRebateCycle(date1, date2) {
			date1 = date1 && dayjs(String(date1)).format('YYYY-MM-DD')
			date2 = date2 && dayjs(String(date2)).format('YYYY-MM-DD')

			if (date1 === date2) {
				return date1 || '-'
			} else {
				return date1 && date2 ? date1 + ' - ' + date2 : '-'
			}
		},
		showLock(rowData) {
			if (!!rowData.auditName) {
				return this.userName === rowData.auditName
			} else {
				return true
			}
		},
		switchPage() {
			this.pageNum = 1
			this.tableData = []
			this.search()
		},
		filterOrderStatus(code, lock) {
			if (lock === 1) {
				return this.activeName === '1'
					? this.$t('funds.fund_audit.review_of_trial_1')
					: this.$t('funds.fund_audit.review_of_trial_2')
			} else {
				const item = this.orderStatusArr.find(
					(item) => item.code === String(code)
				)
				return item ? item.description : '-'
			}
		},
		loadData() {
			if (!this.activeName) return
			this.loading = true
			this.lockedArr = []
			const [startTime, endTime] = this.queryTime || []
			let params = {
				...this.queryData,
				// qryType: this.activeName * 1,
				auditStatus: this.activeName == '1' ? [0] : [2], // 一审 二审
				cycleStartDate: startTime
					? dayjs(startTime)
							.format('YYYY-MM-DD')
							.replace(/\-/g, '')
					: undefined,
				cycleEndDate: endTime
					? dayjs(endTime)
							.format('YYYY-MM-DD')
							.replace(/\-/g, '')
					: undefined
			}
			params = {
				...this.getParams(params)
			}
			if (this.dateFlag == '0') {
				delete params.topProxyName
				delete params.orderNo
			} else if (this.dateFlag == '1') {
				delete params.proxyName
				delete params.orderNo
			} else if (this.dateFlag == '2') {
				delete params.proxyName
				delete params.topProxyName
			}
			this.$api
				.proxyIncomeList(params)
				.then((res) => {
					this.now = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
					const { record, totalRecord } = res.data || {}
					this.loading = false
					this.tableData = record || []
					this.total = totalRecord || 0
					if (this.tableData) {
						// 未显示勾选数量
						let unableNum = 0
						this.tableData.forEach((item) => {
							item.lockOrder = Boolean(item.lockStatus)
							// 判断已经被当前账号锁单的记录
							if (
								item.lockStatus === 1 &&
								this.userName === item.auditName
							) {
								this.lockedArr.push(item)
							}

							// 未显示勾选的记录
							if (
								item.lockStatus === 1 &&
								!(this.userName === item.auditName)
							) {
								unableNum++
							}
						})
						this.lockedAllStatus =
							this.tableData.length ===
							this.lockedArr.length + unableNum
						this.lockedAllDisabled =
							unableNum === this.tableData.length
					}
				})
				.catch(() => {
					this.loading = false
				})
		},

		openDetail(rowData) {
			this.allowAudit = rowData.auditName == this.userName
			this.detailData = rowData
			this.pageType = 'd'
		},
		goBack() {
			this.pageType = 'i'
			this.loadData()
			// this.tableData = []
			// 	this.search()
		},
		dialogData(row, i) {
			this.curData = row
			console.log(i, '2', this.curData)
			if (i == 0) {
				this.incomeDetailDialogVisible = true
			} else if (i == 1) {
				this.auditDetailDialogVisible1 = true
			} else if (i == 2) {
				this.auditDetailDialogVisible2 = true
			} else if (i == 3) {
				this.auditDetailDialogVisible3 = true
			} else if (i == 4) {
				this.auditDetailDialogVisible4 = true
			} else if (i == 5) {
				this.auditDetailDialogVisible5 = true
			}
		},
		reset() {
			this.queryData = {
				minIncomeAmount: undefined,
				maxIncomeAmount: undefined
			}
			this.queryTime = [start(), end()]
			this.pageNum = 1
			this.dateFlag = '0'
			this.search()
		},
		reloadTime() {
			this.queryTime = [start(), end()]
		},
		handleCurrentChange(val) {
			this.pageNum = val
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		handleSizeChange(val) {
			this.pageSize = val
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		handleLockOrder(data) {
			const loading = this.$loading({
				lock: true,
				text: 'Loading',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.3)'
			})
			this.$api
				.proxyIncomeAuditLock(data)
				.then(() => {
					this.tableData = this.tableData.filter(() => true)
					this.$message({
						type: 'success',
						message: this.$t('common.action_success')
					})
					loading.close()
					this.pageNum = 1
					this.search()
				})
				.catch(() => {
					this.tableData = this.tableData.filter(() => true)
					this.search()
					loading.close()
				})
		},
		lockOne(status, id) {
			const data = {
				idList: [id],
				lockStatus: status ? 1 : 0,
				lockType: 1
			}
			this.handleLockOrder(data)
		},
		lockAll(status) {
			const lockIds = []
			this.tableData.forEach((item) => {
				if (item.auditName && item.auditName == this.userName) {
					lockIds.push(item.orderNo)
				} else {
					lockIds.push(item.orderNo)
				}
			})
			const data = {
				idList: lockIds,
				lockStatus: status ? 1 : 0,
				lockType: 2
			}
			this.handleLockOrder(data)
		},
		openBatchReviewDialog() {
			this.batchReviewDialog.formData.ids = []
			if (this.lockedArr.length > 0) {
				this.batchReviewDialog.visible = true
				this.batchReviewDialog.pageType = 'i'
				this.batchReviewDialog.amounts = {}
				this.batchReviewDialog.formData.remark = undefined
				this.batchReviewDialog.totalRebateAmount = 0
				this.batchReviewDialog.totalRecords = this.lockedArr.length
				const amounts = {}
				this.lockedArr.forEach((item) => {
					this.batchReviewDialog.formData.ids.push(item.orderNo)
					this.batchReviewDialog.totalRebateAmount +=
						item.realIncomeAmount
					if (amounts[item.currency]) {
						amounts[item.currency] = new Decimal(
							amounts[item.currency]
						)
							.add(item.realIncomeAmount)
							.toNumber()
					} else {
						amounts[item.currency] = new Decimal(
							item.realIncomeAmount
						).toNumber()
					}
				})
				this.batchReviewDialog.amounts = amounts
			}
		},
		onBatchReviewDialogClose() {
			this.$refs.batchAuditForm.resetFields()
		},
		handleBatchReview() {
			this.batchReviewDialog.loading = true
			const params = {
				auditStatus: this.batchReviewDialog.btnType,
				ids: this.batchReviewDialog.formData.ids,
				remark: this.batchReviewDialog.formData.remark
			}

			this.$api
				.proxyIncomeAuditRecord(params)
				.then((res) => {
					this.$message.success(
						this.$t('funds.fund_audit.batcha_audit_success')
					)
					this.batchReviewDialog.visible = false
					this.batchReviewDialog.loading = false
					this.search()
				})
				.catch(() => {
					this.batchReviewDialog.loading = false
				})
		},
		filterCycleDate(start, end) {
			start = start || '-'
		},
		// 提交列设置数据
		handleOrderList() {
			this.colSettings.visible = false
			this.updateIndexDB()
		},
		// 打开列设置弹框
		openColSettings() {
			this.getIndexDB(this.activityListField)
			this.colSettings.visible = true
		},
		clickDel() {
			this.dialogColumnData = {}
			this.dialogColumnData = {
				// 锁单: true,
				// 订单号: true,
				// 返点周期: true,
				// 代理账号: true,
				// 所属商户: true,
				// 返点金额: true,
				// 审核状态: true,
				// 锁单时间: true,
				// 审核人: true,
				// 申请时间: true.
				[this.$t('funds.fund_audit.lock')]: true,
				[this.$t('funds.order_no')]: true,
				[this.$t('funds.apply_time')]: true,
				[this.$t('funds.fund_audit.rebates_cycle')]: true,
				[this.$t('funds.proxy_name')]: true,
				[this.$t('common.belong_merchant')]: true,
				[this.$t('funds.fund_audit.amount_of_rebates')]: true,
				[this.$t('funds.fund_audit.audit_status')]: true,
				[this.$t('funds.fund_audit.lock_time')]: true,
				[this.$t('funds.fund_audit.lock_account')]: true,
				[this.$t('funds.apply_time')]: true
			}
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	text-align: center;
	color: #909399;
	font-weight: 700;
	border-bottom: 1px solid #00000015;
}
::v-deep .caret-wrapper {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
.divBox {
	display: flex;
	.el-select {
		width: 100px;
	}
}
.data-refresh {
	margin-top: 0;
	padding-bottom: 20px;
	line-height: 28px;
	margin-right: 20px;
}
.grid-content {
	height: 45px;
	line-height: 45px;
	padding: 2px 22px;
	border: 1px solid #ebeef5;
}
.batch-review {
	.el-dialog__title {
		font-weight: 550;
	}
	.el-dialog__body {
		padding: 10px 20px;
	}
	.review-wrapper {
		height: auto;
		display: flex;
		flex-direction: column;
		border: 1px solid rgba(0, 0, 0, 0.164);
		.top {
			border-bottom: 1px solid rgba(0, 0, 0, 0.164);
			.check-details {
				padding: 10px;
				font-size: 15px;
				font-weight: 600;
				span {
					font-size: 12px;
					color: #038eff;
					cursor: pointer;
				}
			}
			.el-row {
				text-align: center;
				.title {
					background-color: rgba(0, 0, 0, 0.08);
					border-top: 1px solid rgba(0, 0, 0, 0.164);
					border-bottom: 1px solid rgba(0, 0, 0, 0.164);
					font-weight: 550;
				}
				.border-right {
					border-right: 1px solid rgba(0, 0, 0, 0.164);
				}

				.el-col {
					height: 40px;
					line-height: 40px;
				}
				.amounts-list {
					border-left: 1px solid rgba(0, 0, 0, 0.164);
					height: auto;
					min-height: 40px;
					padding: 10px 0;
					line-height: 20px;
				}
			}
		}
		.bottom {
			flex: 1;
			.remark-wrapper {
				display: flex;
				height: 100%;
				.left {
					display: flex;
					flex-direction: column;
					text-align: center;
					justify-content: space-around;
					width: 100px;
					border-right: 1px solid rgba(0, 0, 0, 0.164);
					position: relative;
					&::after {
						content: '*';
						color: red;
						position: absolute;
						top: 72px;
						left: 12px;
					}
				}
				.right {
					flex: 1;
					padding: 12px 10px 0px;
				}
			}
		}
	}

	.detail-header {
		.title {
			font-weight: 600;
			font-size: 15px;
		}
		display: flex;
		justify-content: space-between;
		border: 1px solid #ebeef5;
		align-items: center;
		padding: 10px;
	}
	::v-deep .el-table {
		margin-bottom: 10px;
	}
	::v-deep .el-textarea__inner {
		border: 0 none;
	}
	::v-deep .el-form-item__error {
		top: 155px;
	}
	::v-deep .el-table {
		margin-bottom: 10px;
	}
}
::v-deep .el-input-group__prepend {
	width: 110px;
}
.center-wrap {
	display: flex;
	align-items: center;
	justify-content: center;
}
.el-input-number_container {
	display: inline-block;
	position: relative;
	.close_icon {
		position: absolute;
		right: 10px;
		top: 50%;
		transform: translateY(-50%);
		cursor: pointer;
		color: #ccc;
	}
}
</style>
